<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use app\admin\model\Eif;
/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 获取精灵信息
     *
     */
    public function index()
    {

        $name = input('param.name');

        if($name=='')
            $this->error('未获取带精灵名');
        $race = Db::name('eif')->where('name',$name)->value('race');
        if(!$race)
            $this->error('找不到精灵'.$name.'，请联系作者添加!');
        $info = Db::name('race')->where(['race'=>['in',$race]])->select();

        $this->success('请求成功',$info);
    }


    public function add()
    {

        $flg = true;


        header ( "Content-Type: text/html; charset=utf-8" );



        // UTF8内简转繁


        $i = 0;

        while (true)
        {


        $filename = "1.sql";

        $handle = fopen($filename, "r");

        $contents = fread($handle, filesize ($filename));

        fclose($handle);


        $list = Db::name('eif')->field('id,name')->where('id>'.$contents)->limit(5)->select();
        if(!$list)
            return;
        $data = [];
        $sql = '';
        $id = 0;
        foreach ($list as $k=>$v)
        {
            $str = $this->curl($v['name']);
            $id = $v['id'];
            if($str=='')
                continue;
            $arr = [];
            $arr['id'] = $v['id'];
            $arr['race'] = $str;
            $data[] = $arr;

            //$sql.="UPDATE `fa_eif` SET `race`='{$str}' WHERE `id` = ".$v['id'].'\r\n';
        }
        if(!$data)
            echo '没有可更新数据';
        //print_r($data);exit;




        $eif = new Eif;

        $res = $eif->saveAll($data);
        if($res)
        {
            $myfile = fopen("1.sql", "w") or die("Unable to open file!");

            //fwrite($myfile, '1.sql');

            fwrite($myfile, $id);
            fclose($myfile);
        }
        //echo $eif->getLastSql();
            $i++;
        echo $i;
        sleep(2);
        }
    }


    public function curl($name)
    {
        //$list = Db::name('eif')->field('id,name')->select();

        $url = "https://wiki.52poke.com/wiki/".$name;

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);


        curl_setopt($ch, CURLOPT_HEADER, 0);

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查

        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在



        $notice = curl_exec($ch);

        $isMatched = preg_match_all('/<span class="type-box-8-inner">(.*?)<\/span>/', $notice, $matches);

        $str = '';
       if($matches)
       {
           $zz = [];
            foreach ($matches[0] as $k=>$v)
            {

                $zz[] = $afterText = \addons\convert\library\Convert::convert(strip_tags($v),false);;
            }

            return implode(',',array_unique($zz));
       }
        return $str;

    }


}
